package com.paw.portal.util;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.paw.common.domain.ProTableResponse;
import org.springframework.beans.BeanUtils;

/**
 * @author Rubble
 * @date 2021/8/21
 **/
public class PageUtil {

  public static <T,M> ProTableResponse<T> convertPage(IPage<M> page,Class<T> klass){
    ProTableResponse<T> proTableResponse = new ProTableResponse();
    proTableResponse.setCurrent(page.getCurrent());
    proTableResponse.setPageSize(page.getSize());
    proTableResponse.setTotal(page.getTotal());

    List<T> list = new ArrayList<>();
    try {
      for (M record : page.getRecords()) {
        T  dto =  klass.newInstance();
        BeanUtils.copyProperties(record,dto);
        list.add(dto);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    proTableResponse.setData(list);

    return proTableResponse;
  }

}
